<?php

class LawyerOrderController extends LawyerCenterController {
	
	
	/**
	 * 全部的咨询
	 */
	public function questionAll() {
		$status = array();
		$questions = $this->getQuestionList($status);
		return $this->makeView('center.lawyer.order.all', $questions);
	}
	
	/**
	 * 获取咨询列表
	 */
	protected function getQuestionList($status) {
		$questionsQuery = Post::leftJoin('post_order', 'post_order.post_id', '=', 'posts.id')
			->leftJoin('order', 'post_order.order_id', '=', 'order.id')
			->leftJoin('post_thread', 'posts.id', '=', 'post_thread.post_id')
			->leftJoin('user', 'user.id', '=', 'posts.creator_id')
			->leftJoin('evaluate', 'evaluate.item_id', '=', 'posts.id')
			->select(
				'posts.id',
				'posts.type',
				'posts.title',
				'posts.content',
				'posts.status',
				'posts.post_category',
				'posts.creator_id',
				'user.avatar as creator_avatar',
				'user.realname as creator_realname',
				'posts.created_at',
				'posts.updated_at',
				'evaluate.score as evaluate_score',
				'evaluate.comment as evaluate_comment',
				'order.order_no')
				->whereIn('posts.type',array(POST::TYPE_PAY_TEXT, POST::TYPE_PAY_PHONE))
				->where('post_thread.user_id', $this->user->id)
				->orderBy('updated_at', 'DESC');
		if (!empty($status)) {
			$questionsQuery->whereIn('posts.status', $status);
		}
		$questions = $questionsQuery->get();
		foreach($questions as $question){
			$question['type'] = $question->getType();
			$question['status_code'] = $question->status;
			$question['status'] = $question->getStatus();
			$question['post_category'] = $question->getPostCatKey();
		}
		return $questions->toArray();
	}
	
	/*
	 * 待受理
	 */
	public function questionNew(){
		$status = array(Post::STATUS_NEW);
		$questions = $this->getQuestionList($status);
		return $this->makeView('center.lawyer.order.new', $questions);
	}
	
	/*
	 * 待确认
	 */
	public function questionReplied(){
		$status = array(Post::STATUS_REPLIED);
		$questions = $this->getQuestionList($status);
		return $this->makeView('center.lawyer.order.replied', $questions);
	}
	
	/*
	 * 已完成
	 */
	public function questionComplete(){
		$status = array(Post::STATUS_WAIT_FOR_EVALUATION,Post::STATUS_COMPLETE);
		$questions = $this->getQuestionList($status);
		return $this->makeView('center.lawyer.order.complete', $questions);
	}
	
	/*
	 * 已拒绝
	 */
	public function questionRefused(){
		$status = array(Post::STATUS_REFUSED);
		$questions = $this->getQuestionList($status);
		return $this->makeView('center.lawyer.order.waitforpay', $questions);
	}
	
	/*
	 * 已取消
	 */
	public function questionCancelled(){
		$status = array(Post::STATUS_CANCELLED);
		$questions = $this->getQuestionList($status);
		return $this->makeView('center.lawyer.order.waitforpay', $questions);
	}
	
	
	/*
	 * 查看咨询的详情
	*/
	public function view($id){
		$question = Post::find($id);
		if (!is_a($question, 'Post')) {
			return $this->makeView('errors.404','该订单不存在', 404);
		}
		$order = $question->getOrder($id);
		$data = [];
		$data ['post_id'] = $id;
		$data ['type'] = $question->getType();
		$data ['price'] = round($order->price);
		$data ['order_no'] = $order->order_no;
		$data ['status_code'] = $question->status;
		$data ['status'] = $question->getStatus();
		$data ['title'] = $question->title;
		$data ['content'] = $question->content;
		$data ['updated_at'] = $question->updated_at;
		$data ['created_at'] = $question->created_at;
    	$data['postAttachs'] = $question->postAttach->toArray();
		return $this->makeView('center.lawyer.order.view', $data);
	}
	
	/*
	 * 接收订单
	 */
	public function accept($id){
		$result =  $this->changeQuestion($id,Post::STATUS_REPLIED);
		if($result) return $this->makeView('general.200','接单成功',200);
		return $this->makeView('errors.503','请求失败',503);
	}
	
	/*
	 * 拒绝订单
	 */
	public function refuse($id){
		$result = $this->changeQuestion($id,Post::STATUS_REFUSED);
		if($result){
			$user_id = Post::find($id)->creator_id;
			$order_id = PostOrder::where('post_id', $id)->first()->order_id;
			$price = Order::find($order_id)->price;
			$userWallet = Wallet::getWallet($user_id);
			$walletTransaction = WalletTransaction::getWalletTransaction(
					$user_id,
					WalletTransaction::TYPE_QUESTION,
					$order_id,
					WalletTransaction::DIRECTION_OUT);
													
			$userWallet->money = $userWallet->money + $price;
			$walletTransaction->status = WalletTransaction::STATUS_FAIL;
			$walletTransaction->result = "该订单已被拒绝，费用已返还到您的钱包";
			DB::transaction(function() use($userWallet, $walletTransaction) {
				$userWallet->save();
				$walletTransaction->save();
			});
			return $this->makeView('general.200','拒绝成功',200);
		}else{
			return $this->makeView('errors.400', '该订单状态不正确', 400);
		}
		return $this->makeView('errors.503','请求失败',503);
		
	}
	
	protected function changeQuestion($id, $status){
		$question = Post::find($id);
		if($question){
			if($question->status == Post::STATUS_NEW){
				$question->status = $status;
				return (bool)$question->save();
			}
		}
		return;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}